﻿using RAP.Framework.Libary.Configuration;
using RAP.Framework.Libary.DataSource;
using System;
using System.Collections;
using System.Linq;
using System.Threading.Tasks;
using RAP.Framework.Libary.Utils;
using RAP.Web.Libary.Service.Attributes;
using RAP.Web.Libary.Service;
using RAP.Web.Libary.Config;
using RAP.Framework.Libary.SqlDataService.Service;
using RAP.Web.Libary.Common;
using RAP.Web.Core.Server.Common.Config;
using RAP.Framework.Libary.DataSource.Model;
using RAP.Framework.Libary.DataSource.Context;

namespace RAP.Web.Core.Server.Service.Common
{
	/// <summary>
	/// 公共服务对象
	/// </summary>
	[ServiceScope( ScopeType = ServiceScopeTypes.Singleton )]
	public class CommonService : APIServiceBase
	{
		protected IDictionary DataSourceConfig => ConfigurationManager.ToDictionary( SiteSettings.DataConfigField );

		private IConfigurationManager ConfigurationManager { get; set; }

		public CommonService( 
			IServiceProvider serviceProvider, 
			IConfigurationManager configurationManager ) : base( serviceProvider )
		{
			ConfigurationManager = configurationManager;
		}

		/// <summary>
		/// 获取城市列表
		/// </summary>
		/// <returns></returns>
		public async Task<object> GetCities( )
		{
			return await this.SqlDataService.ExecuteNoCache();
		}

		/// <summary>
		/// 初始化城市数据源配置
		/// </summary>
		/// <param name="param"></param>
		/// <returns></returns>
		public bool InitCityDataSources( )
		{
			var res = this.SqlDataService.ExecuteSync( allowFireEvent: false );
			if ( res.HasError() )
			{
				Console.ForegroundColor = ConsoleColor.Red;
				Console.WriteLine( "城市数据源初始化失败！" );
				Console.ForegroundColor = ConsoleColor.White;
				return false;
			}
			var dataSourceConfig = this.DataSourceConfig.ToDictionary( SiteSettings.DataSourcesField );
			var cities = res.GetFirstList();
			if ( cities.IsNotNullOrEmpty() )
			{
				foreach ( IDictionary city in cities )
				{
					var dataSource = city.ToString( "conn_str" ).ToDictionary();
					var cityCode = city.ToString( FieldConsts.IdField );
					dataSourceConfig[ $"{SiteSettings.DataSourceKeyPrefix}{cityCode}" ] = dataSource;
				}
			}
			Console.WriteLine( "城市数据源初始化成功！" );
			return true;
		}

		/// <summary>
		/// 获取有效的城市编码
		/// </summary>
		/// <param name="param"></param>
		/// <returns></returns>
		public async Task<object> GetValidCityCode( IQueryParams param )
		{
			var res = await this.SqlDataService.ExecuteNoCache( param, allowFireEvent: false );
			if ( res.HasError() )
			{
				return res;
			}
			return res.GetFirstResultId();
		}

		/// <summary>
		/// 获取省份数据
		/// </summary>
		/// <returns></returns>
		public async Task<object> GetProvinces( )
		{
			var res = await this.SqlDataService.ExecuteNoCache();
			if ( res.HasError() )
			{
				return res;
			}
			return res.GetFirstRecordset();
		}

		/// <summary>
		/// 获取开放的城市列表
		/// </summary>
		/// <returns></returns>
		public async Task<object> GetOpenCitys( )
		{
			var res = await this.SqlDataService.ExecuteNoCache();
			if ( res.HasError() )
			{
				return res;
			}
			return res.GetFirstRecordset();
		}
	}
}
